package com.chen.schedule.filters;

import javax.servlet.*;
import javax.servlet.annotation.WebFilter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 检验用户是否进行登录
 */
@WebFilter({"/showSchedule.html", "/schedule/*"})
public class LoginFilter implements Filter {
    @Override
    public void doFilter(ServletRequest servletRequest, ServletResponse servletResponse, FilterChain filterChain) throws IOException, ServletException {
        // 父转子使用子的api
        HttpServletRequest request = (HttpServletRequest) servletRequest;
        HttpServletResponse response = (HttpServletResponse) servletResponse;
        // 将session中的username数据获取出来
        Object username = request.getSession().getAttribute("username");
        if (username == null) {
            // 获取的数据为nul ， 则说明没有登录
            response.sendRedirect("/login.html");
        } else {
            // 获取的到了数据，登录了
            filterChain.doFilter(request, response);
        }
    }
}
